#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <utmp.h>

int main(){
    //创建套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    //向服务器（特定的IP和端口）发起请求
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //每个字节都用0填充
    serv_addr.sin_family = AF_INET;  //使用IPv4地址
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //具体的IP地址
    serv_addr.sin_port = htons(1234);  //端口
    connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
   
    //读取服务器传回的数据
    struct  utmp buffer;
    FILE    *fp_recv_data;

    fp_recv_data = fopen( "./recv_data.txt" , "w");
    printf("Message from server:\n");
    while (read(sock, &buffer, sizeof(buffer)))
    {
    	printf("%d      %d      %d      %s      %s\n", buffer.ut_tv.tv_sec, buffer.ut_type,
			      buffer.ut_addr_v6[4], buffer.ut_user, buffer.ut_host);
	fwrite(&buffer, sizeof(buffer), 1, fp_recv_data);
    }
    fclose(fp_recv_data);
   
    //关闭套接字
    close(sock);

    return 0;
}
